﻿using Business.Service;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcAppWeb.Controllers
{
    public class GoodsMoveController : BaseDataController
    {
        //
        // GET: /GoodsMove/

        public ActionResult GoodsMove()
        {
            if (Session["UserInfo"] == null) return RedirectToAction("LogOn", "Account");
            else
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                return View(model);

            }
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="sBatchNO"></param>
        /// <param name="sGoodsCode"></param>
        /// <param name="sFlag"></param>
        /// <param name="checkTimeS"></param>
        /// <param name="checkTimeE"></param>
        /// <param name="cellNo"></param>
        /// <param name="sGoodsName"></param>
        /// <returns></returns>
        public JsonResult InitJsonResult(string goodsMoveID, string checkTimeS, string checkTimeE, string sGoodsCode, string sGoodsName)
        {
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());
            int total = 0;
            string where = " where 1=1 ";


            if (!string.IsNullOrEmpty(goodsMoveID))
            {
                where += " and g.GOODSMOVEID='" + goodsMoveID + "'";
            }
            if (!string.IsNullOrEmpty(checkTimeS))
            {
                where += " and g.MOVEDATE>= to_date('" + checkTimeS + "','yyyy-mm-dd hh24:mi:ss')";
            }
            if (!string.IsNullOrEmpty(checkTimeE))
            {
                where += " and g.MOVEDATE<= to_date('" + checkTimeE + "','yyyy-mm-dd hh24:mi:ss')";
            }
            if (!string.IsNullOrEmpty(sGoodsCode))
            {
                where += @" and g.GOODSMOVEID in(select distinct t1.goodsmoveid from lmgoodsmoveitem t1 inner join goods t2 on t1.goodsid=t2.goodsid
                            where t2.goodscode like '%" + sGoodsCode + "%')";
            }
            if (!string.IsNullOrEmpty(sGoodsName))
            {
                where += @" and g.GOODSMOVEID in(select distinct t1.goodsmoveid from lmgoodsmoveitem t1 inner join goods t2 on t1.goodsid=t2.goodsid
                            where t2.goodsname like '%" + sGoodsName + "%')";
            }
            where += @" order by g.GOODSMOVEID ";


            BGoodsMove bi = new BGoodsMove();
            var result = bi.GetGoodsMoveList(page, rows, out total, where);

            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", total);
            json.Add("rows", result);
            return Json(json, JsonRequestBehavior.AllowGet);
        }




        [HttpPost]
        public JsonResult QueryDetailData(string GoodsMoveID)
        {

            BGoodsMove bi = new BGoodsMove();
            var result = bi.GetGoodsMoveItemByGoodsMoveID(GoodsMoveID);

            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("rows", result);
            return Json(json, JsonRequestBehavior.AllowGet);


        }
    }
}
